package com.ruoyi.quartz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.quartz.mapper.OrderListMapper;
import com.ruoyi.quartz.service.IOrderListService;
import com.ruoyi.system.domain.DTO.OrderListDto;
import com.ruoyi.system.domain.vo.OrderLogVo;
import com.ruoyi.system.service.IRyAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class OrderListServiceImpl implements IOrderListService {
    //测试
    public static final String URLSTR = "https://api-pp.lines.coscoshipping.com/service";
    public static final String HMACUSERNAME = "c4c096fefbb97c98facd8b51982397a8";
    public static final String HMACSECRET = "49deeddc99b6884d5c1ab3541a22e526";

//    //正式
//    public static final String URLSTR = "https://api.lines.coscoshipping.com/service";
//    public static final String HMACUSERNAME = "6851c92c09b846f1488496704d855c64";
//    public static final String HMACSECRET = "9c2d704dbf8dc69059ebbbf2eb92b4c2";

    Charset utf8Charset = Charset.forName("utf-8");
    Charset iso88591Charset = Charset.forName("iso-8859-1");

    @Autowired
    private OrderListMapper orderListMapper;
    @Autowired
    private IRyAdminService iRyAdminService;

    @Override
    public List<OrderLogVo> list(OrderListDto orderListDto) {
        String curName = Thread.currentThread().getName() ;
        System.out.println("当前时间:"+new Date()+"  任务executelist对应的线程名: "+curName);
        List<OrderLogVo> orderLogVoList = new ArrayList<>();
        orderLogVoList = orderListMapper.list(orderListDto);
        for (OrderLogVo forOrderLogVo:orderLogVoList) {
            if(forOrderLogVo.getTwentyGPCount() == null || forOrderLogVo.getTwentyGPCount().trim().length() == 0){
                forOrderLogVo.setTwentyGPCount("-");
                forOrderLogVo.setTwentyGPPrice("-");
            }
            if(forOrderLogVo.getFortyGPCount() == null || forOrderLogVo.getFortyGPCount().trim().length() == 0){
                forOrderLogVo.setFortyGPCount("-");
                forOrderLogVo.setFortyGPPrice("-");
            }
            if(forOrderLogVo.getFortyHQCount() == null || forOrderLogVo.getFortyHQCount().trim().length() == 0){
                forOrderLogVo.setFortyHQCount("-");
                forOrderLogVo.setFortyHQPrice("-");
            }
            if(forOrderLogVo.getTwentyRFCount() == null || forOrderLogVo.getTwentyRFCount().trim().length() == 0){
                forOrderLogVo.setTwentyRFCount("-");
                forOrderLogVo.setTwentyRFPrice("-");
            }
            if(forOrderLogVo.getFortyRQCount() == null || forOrderLogVo.getFortyRQCount().trim().length() == 0){
                forOrderLogVo.setFortyRQCount("-");
                forOrderLogVo.setFortyRQPrice("-");
            }
            if(forOrderLogVo.getRemark() == null || forOrderLogVo.getRemark().trim().length() == 0){
                forOrderLogVo.setRemark("-");
            }

            forOrderLogVo.setTwentyGPCount(forOrderLogVo.getTwentyGPCount()+"/"+forOrderLogVo.getTwentyGPPrice());
            forOrderLogVo.setFortyGPCount(forOrderLogVo.getFortyGPCount()+"/"+forOrderLogVo.getFortyGPPrice());
            forOrderLogVo.setFortyHQCount(forOrderLogVo.getFortyHQCount()+"/"+forOrderLogVo.getFortyHQPrice());
            forOrderLogVo.setTwentyRFCount(forOrderLogVo.getTwentyRFCount()+"/"+forOrderLogVo.getTwentyRFPrice());
            forOrderLogVo.setFortyRQCount(forOrderLogVo.getFortyRQCount()+"/"+forOrderLogVo.getFortyRQPrice());

//            if(forOrderLogVo.getConfirmStatus().equals("未确认")){
//                if(forOrderLogVo.getOrderNo()!=null && !forOrderLogVo.getOrderNo().equals("") && forOrderLogVo.getOrderNo().length()>0){
//
//                    //请求接口去查订单确认状态
//                    try {
//                        HttpClientCall httpClientCall = new HttpClientCall();
//                        //账号密码
//                        String hmacUsername = HMACUSERNAME;
//                        String hmacSecret = HMACSECRET;
//                        //请求路径
//                        String url = URLSTR + "/synconhub/order/detail/"+forOrderLogVo.getOrderNo();
//                        if (forOrderLogVo.getProductType().equals("远期产品")){
//                            url = URLSTR + "/synconhub/order/longTerm/detail/"+forOrderLogVo.getOrderNo();
//                        }
//                        //请求格式
//                        String method = "GET";
//                        //触发请求
//                        String result  = httpClientCall.callService(url, null, hmacUsername, hmacSecret, method);
//                        JSONObject resultJsonObject = JSONObject.parseObject(result);
//                        Map<String, Object> map = (Map<String, Object>) resultJsonObject;
//                        if (map.get("code").toString().equals("0")) {//成功查询数据
//                            JSONObject data = new JSONObject();
//                            data = JSON.parseObject(map.get("data").toString());
//                            if(data.get("orderStatus").toString().equals("CONFIRMED")){
//                                forOrderLogVo.setConfirmStatus("已确认");
//                                //修改
//                                int num = orderListMapper.updateOrderLogVo(forOrderLogVo);
//                                if(num > 0){
//                                    System.out.println("成功。orderLog："+JSON.toJSON(forOrderLogVo));
////                                    int resultNum = iRyAdminService.updateOrderLogVo(forOrderLogVo);
////                                    if(resultNum>0){
////                                        System.out.println("成功。orderLog："+JSON.toJSON(forOrderLogVo));
////                                    }else{
////                                        System.out.println("失败。orderLog："+JSON.toJSON(forOrderLogVo));
////                                    }
//                                }else{
//                                    System.out.println("失败。orderLog："+JSON.toJSON(forOrderLogVo));
//                                }
//                            }
//                        }
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
        }
        return orderLogVoList;
    }
}
